// pages/InitiateActivity/InitiateActivity.js
var util = require('../../utils/util.js');
var app = getApp();
var now = new Date();
Page({

  /**
   * 页面的初始数据
   */
	data: {
		imgs: '', //上传图片的url路径
		//长按事件
		touchStartTime: 0, // 触摸开始时间
		touchEndTime: 0, // 触摸结束时间
		choosedidian: '请选择活动地点',
		uploaderImg: "/images/upload.png",

		array_area: [{
			id: 1,
			name: '信息'
		},
		{
			id: 2,
			name: '宣传'
		},
		{
			id: 3,
			name: '调解'
		},
		{
			id: 4,
			name: '其他'
		},
		{
			id: 5,
			name: '巡防'
		}
		],
		array_tel: [{
			id: 1,
			name: '是'
		},
		{
			id: 2,
			name: '否'
		},
		],
		array_qun: [{
			id: 1,
			name: '无'
		},],
		array_quyu: [],
		index_area: 0,
		start_date: util.formatDate(now),
		start_time: util.formatTimeOfHm(now),
		index_tel: 1,
		index_quyu: 0,
		index_qun: 0,
		end_date: util.formatDate(now),
		end_time: util.formatTimeOfHm(now),
	},

	bindPickerChangeArea: function (e) {
		let that = this;
		let index = e.detail.value;
		// let array_area_id = that.data.array_area[index].id;
		that.setData({
			index_area: index
		})
		// console.log(array_area_id);
	},
	// 手机绑定
	trueorfalse: function (e) {
		let that = this;
		let index = e.detail.value;
		// let array_area_id = that.data.array_area[index].id;
		that.setData({
			index_tel: index
		})
		// console.log(array_area_id);
	},
	//活动区域
	huodongquyu: function (e) {
		let that = this;
		let index = e.detail.value;
		// let array_area_id = that.data.array_area[index].id;
		that.setData({
			index_quyu: index
		})
		// console.log(array_area_id);
	},
	// 所属群组
	suoshuqunzu: function (e) {
		let that = this;
		let index = e.detail.value;
		// let array_area_id = that.data.array_area[index].id;
		that.setData({
			index_qun: index
		})
		// console.log(array_area_id);
	},
	bindStartDateChange(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			start_date: e.detail.value
		})
	},
	bindStartTimeChange(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			start_time: e.detail.value
		})
	},

	bindEndDateChange(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			end_date: e.detail.value
		})
	},
	bindEndTimeChange(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			end_time: e.detail.value
		})
	},

	selectImg: function () {
		let that = this;
		var data = {
			count: 1
		}
		app.upImg(data, function (res) {
			that.setData({
				imgs: res
			});
		})
	},

	// 按钮触摸开始触发的事件
	touchStart: function (e) {
		this.touchStartTime = e.timeStamp
	},
	// 按钮触摸结束触发的事件
	touchEnd: function (e) {
		this.touchEndTime = e.timeStamp
	},
	// 长按
	longTap: function (e) {
		let that = this;
		wx.showModal({
			title: '提示',
			content: '确定要删除？',
			success: function (res) {
				if (res.confirm) {
					that.setData({
						imgs: ''
					});
				}
			}
		})
	},


  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {

	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
		let that = this;
		// 群组包括发起和参与
		var memberGroup = [];
		// 发起
		var data = {
			paraUrl: 'group/list/member',
			paraData: ''
		}
		app.getRequest(data, function (res) {
			res.data.body.forEach(item => {
				memberGroup.push({
					id: item.id,
					code: item.code,
					name: item.name,
					logo: item.logo
				})
			})
			// 参与, 需筛选与发起不重复的. 必须同步调用, 否则会出现重复数据
			var joinGroupData = {
				paraUrl: 'group-member/list/member',
				paraData: ''
			}
			app.getRequest(joinGroupData, function (res) {
				var existGroup = JSON.stringify(memberGroup)
				res.data.body.forEach(item => {
					if (existGroup.indexOf(item.group.id) == -1) {
						// 不重复则构造对象
						memberGroup.push({
							id: item.group.id,
							code: item.group.code,
							name: item.group.name,
							logo: item.group.logo
						})
					}
				})
				that.setData({
					array_qun: memberGroup
				})
			})
		})

		var data1 = {
			paraUrl: 'region/applied/default',
			paraData: ''
		}
		app.getRequest(data1, function (res) {
			that.setData({
				array_quyu: res.data.body
			});
		})
	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () {

	},

  /**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function () {

	},

  /**
   * 用户点击右上角分享
   */
	onShareAppMessage: function () {

	},
	//点击保存
	formBindsubmit: function (e) {


		var that = this;
		if (this.data.array_tel[this.data.index_tel].name == "是") {
			var array_tel = true;
		} else {
			var array_tel = false;
		}
		if (this.data.location == undefined) {
			wx.showToast({
				title: '请选择地址',
				icon: 'success',
				image: '/images/shibai.png',
				duration: 2000
			})
			return;
		}
		// 群组非必填
		var groupId = '';
		if (this.data.array_qun[this.data.index_qun]) {
			groupId = this.data.array_qun[this.data.index_qun].id;
		}
		// 时间转时间戳
		var kaishi = this.data.start_date + ' ' + this.data.start_time;
		var jieshu = this.data.end_date + ' ' + this.data.end_time;
		var startTime = Date.parse(new Date(kaishi));
		var endTime = Date.parse(new Date(jieshu));
		var formwhere = wx.getStorageSync("formwhere")
		if (formwhere == 1) {
			var activity = true
		} else if (formwhere == 2) {
			var activity = false
		}
		var paraData = {
			"name": e.detail.value.name, // 活动名字
			"desc": e.detail.value.desc, // 活动描述
			"mainImageUrl": that.data.imgs.fileUrl, // 活动主图
			"type": this.data.array_area[this.data.index_area].name, // 活动类型, 写成常量即可, 和搜索条件一样
			"activity": activity, // 是否活动, 否则为任务
			"mobileBind": array_tel, // 是否需要绑定手机才可以参加活动
			"attendanceLimit": e.detail.value.attendanceLimit, // 报名人数上限
			"region": this.data.array_quyu[this.data.index_quyu],
			"address": this.data.location.address, // 地图点选的活动地点
			"latitude": this.data.location.latitude, // 地图点选的纬度
			"longitude": this.data.location.longitude, // 地图点选的经度
			"checkInArea": e.detail.value.checkInArea, // 签到范围, 单位为KM
			"startTime": startTime, // 活动开始时间
			"endTime": endTime // 活动结束时间

		}
		if (paraData.name == "" || paraData.desc == "" || paraData.mainImageUrl == "" || paraData.attendanceLimit == "" || paraData.checkInArea == "") {
			wx.showToast({
				title: '请将数据填写完整',
				icon: 'success',
				image: '/images/shibai.png',
				duration: 2000
			})
			return;
		}
		var data = {
			paraUrl: 'task/create?groupId=' + groupId,
			paraData: paraData
		}
		app.postRequest(data, function (res) {
			if (res.data.result == 200) {
				wx.showToast({
					title: '添加成功',
					icon: 'success',
					duration: 2000
				})
				setTimeout(function () {
					wx.reLaunch({
						url: "../Activity/activity"
					})
				}, 2000)
			} else {
				wx.showToast({
					title: '此标签已经存在',
					icon: 'success',
					image: '/images/shibai.png',
					duration: 2000
				})
			}
		})
	},
	// 地图
	selectLocation: function (e) { //自行定义tap事件
		var that = this
		wx.chooseLocation({ //微信API--打开地图选择位置。
			success: function (res) { //成功之后，目前只返回这四组参数打开地图选择位置。
				that.setData({
					location: res
				})
			},
			fail: function (error) {
				console.log(error)
			},
			complete: function (e) {
				console.log(e)
			}
		})
	}
})